package sicnu.cs.ich.log.schedule;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import sicnu.cs.ich.log.service.ILogService;

import java.io.IOException;

/**
 * @author CaiKe
 * @date 2021/12/8 20:38
 */

@Slf4j
@Component
@RequiredArgsConstructor
public class LogSaveSchedule {
    private final ILogService logService;

    @Scheduled(cron = "0 0/1 * * * ? ")
    public void saveOperationLog() {
        log.info("定时任务触发，保存操作日志");
        try {
            logService.saveOperationLogs();
        } catch (IOException e) {
            log.error("保存操作日志出错");
            e.printStackTrace();
        }
    }

    @Scheduled(cron = "10 0/5 * * * ? ")
    public void saveLoginLog() {
        log.info("定时任务触发，保存登录日志");
        try {
            logService.saveLoginLogs();
        } catch (IOException e) {
            log.error("保存登录日志出错");
            e.printStackTrace();
        }
    }

    /**
     * 删除5分钟前的SQL文件
     */
    @Scheduled(cron = "30 0/10 * * * ? ")
    public void deleteUselessOperationSQLs() {
        logService.deleteOperationSQL(10 * 60L);
    }


    /**
     * 删除10分钟前的SQL文件
     */
    @Scheduled(cron = "30 0/30 * * * ? ")
    public void deleteUselessLoginSQLs() {
        logService.deleteLoginSQL(30 * 60L);
    }


}